package primalgorithms.design;

import java.util.Random;

public class _01Shuffle {
    private int[] nums;
    private Random random;

    public _01Shuffle(int[] nums) {
        this.nums = nums;
        random = new Random();
    }

    public int[] reset() {
        return nums;
    }

    public int[] shuffle() {
        if (nums == null){
            return null;
        }
        int[] mir = nums.clone();
        for (int i = 0;i < mir.length;i++){
            int j = random.nextInt(i + 1);
            swap(mir,i,j);
        }
        return mir;
    }

    private void swap(int[] mir, int i, int j) {
        if (i != j){
            mir[i] = mir[i] + mir[j];
            mir[j] = mir[i] - mir[j]; //mir[i]
            mir[i] = mir[i] - mir[j];
        }
    }
}
